//#include <unistd.h>  // for F_ok
#include <glib.h>
#include <stdio.h> 
#include <unistd.h>

#include "config.h"

static int webprint_client_pid = 0;

static int __start_webprint_keeper(void)
{
	printf("Start service engine by client\n");
	int pid = 0;
	int ret = 0;
	int status = 0;
	
	while (1) {
		webprint_client_pid = -1;
		/*
		//need <unistd.h>
		if (access(WEBPRINTER_CLIENT_EXE, F_OK) != 0) {
			return -1;
		}*/
		pid = fork();
		switch (pid) {
		case -1: // error
			printf("fail to start ASF service engine \n");
			sleep(10);
			break;

		case 0: //child, need return
			printf("webprint_client running from keeper\n");
			setsid(); // let it be new one
			execl(WEBPRINTER_CLIENT_EXE,"webprint_client", NULL);
			return;

		default://parent
			webprint_client_pid = pid;
			pid = wait(&status);
			if (webprint_client_pid == pid) {
				printf("webprint_client pid[%d] is dead\n",pid);
			}
			break;
		}
	}
	return pid;
}

int main(int argc,char *argv[])  
{  
	__start_webprint_keeper();
    return 0;  
}
//signal(SIGCHLD,SIG_IGN);  //let child to free itself